﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class UIScripts : MonoBehaviour
{
    [Header("按钮")]
    public Button btnget;
    public Button btngetparm;
    public Button btnjson;
    public Button btnpost;
    public Button btntcp;
    public Button btnudp;

    [Space]
    [Header("显示")]
    public Text txtshow;

    [Space]
    [Header("输入框")]
    public InputField edturl,edtport;
    public InputField edtparm;
    
    //定义一个返回数据的Action
    private Action<bool, string> actionRes;
    // Start is called before the first frame update
    void Start()
    {
        InitAction();

        //Get按钮操作
        btnget.onClick.AddListener(() =>
        {
            Debug.Log(edturl.text);
            string url = edturl.text;
            HttpRestful.Instance.Get(url, actionRes);
        });
        btngetparm.onClick.AddListener(() =>
        {
            string url = edturl.text;
            string param = edtparm.text;

            string allurl = url + "/Info?Summary=" + param;
            HttpRestful.Instance.Get(allurl, actionRes);
        });

        btnjson.onClick.AddListener(() => StartCoroutine(JsonConvert()));

        btnpost.onClick.AddListener(() =>
        {
            WeatherForecast item = new WeatherForecast();
            item.Summary = "Alvin";
            item.Date = DateTime.Now;
            item.TemperatureC = 10;
            item.TemperatureF = 20;
            string json = JsonUtility.ToJson(item);

            string url = edturl.text + "/Reg";
            Debug.Log(url);
            HttpRestful.Instance.Post(url, json, actionRes);
        });

        btntcp.onClick.AddListener(() =>
        {
            SocketTcp.Instance.Connect(edturl.text, Int32.Parse(edtport.text)).Send("1234567").Recv(actionRes);
        });

        btnudp.onClick.AddListener(() =>
        {
            SocketUdp.Instance.Connect(edturl.text, Int32.Parse(edtport.text)).Send("udpsendmsg").Recv(actionRes);
        });
    }

    /// <summary>
    /// 写返回Action的处理方法
    /// </summary>
    private void InitAction()
    {
        actionRes = new Action<bool, string>((bl, str) =>
        {
            if (bl)
            {
                txtshow.text = str;
            }
            else
            {
                string resjson = "{\"array\":" + str + "}";
                txtshow.text = resjson;
                WeatherData lists = JsonUtility.FromJson<WeatherData>(resjson);
                StringBuilder sb = new StringBuilder();
                foreach (WeatherForecast item in lists.array)
                {
                    sb.Append("Date:" + item.Date + " Summary:" + item.Summary + " TemperatureF:"
                        + item.TemperatureF + "TemperatureC:" + item.TemperatureC + "\r\n");
                }
                txtshow.text = sb.ToString();
            }
        });
    }

    IEnumerator JsonConvert()
    {
        WeatherForecast item = new WeatherForecast();
        item.Summary = "Alvin";
        item.Date = DateTime.Now;
        item.TemperatureC = 10;
        item.TemperatureF = 20;

        string json = JsonUtility.ToJson(item);
        txtshow.text = json;
        yield return new WaitForSeconds(3f);

        WeatherForecast newitem = JsonUtility.FromJson<WeatherForecast>(json);
        string showtext = "Summary:" + newitem.Summary + "  Date:" + newitem.Date +
            "  C:" + newitem.TemperatureC + "  F:" + newitem.TemperatureF;
        txtshow.text = showtext;
    }

}
